package com.company.cloud.admin.modules.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.admin.constant.PlatformAdminConstant;
import com.company.cloud.admin.modules.admin.controller.req.ManageUserAddParam;
import com.company.cloud.admin.modules.admin.controller.req.ManageUserUpdateInfoParam;
import com.company.cloud.admin.modules.admin.service.UserManageMgrService;
import com.company.cloud.admin.modules.base.entity.UserManageInfo;
import com.company.cloud.admin.modules.base.entity.UserRoleInfo;
import com.company.cloud.admin.modules.base.service.UserManageInfoService;
import com.company.cloud.admin.modules.base.service.UserRoleInfoService;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.constant.BaseConstant;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.PasswordUtils;

@Service
public class UserManageMgrServiceImpl implements UserManageMgrService {


    @Autowired
    private UserManageInfoService userManageInfoService;
    @Autowired
    private UserRoleInfoService userRoleInfoService;


    @Transactional
    @Override
    public void save(ManageUserAddParam params, LoginSimpleUserInfo userInfo) {
        ValidatorUtils.validate(params);
        UserManageInfo oldEntity= userManageInfoService.getOne(
                new QueryWrapper<UserManageInfo>()
                        .eq("user_name",params.getUserName()));
        if(oldEntity!=null){
            throw  new BusinessException(BaseReturnEnum.DATA_EXISTS);
        }
        UserManageInfo entity=new UserManageInfo();
        entity.setUserName(params.getUserName());
        entity.setNickName(params.getNickName());
        entity.setRealName(params.getRealName());
        entity.setHeadImg(params.getHeadImg());
        entity.setUserState(PlatformAdminConstant.AdminUserState.AUS_Open.getCode());
        String[] passwords= PasswordUtils.getMd5Password(params.getPassword());
        entity.setPassword(passwords[0]);
        entity.setSalt(passwords[1]);
        entity.setMobile(params.getMobile());
        entity.setTel(params.getTel());
        entity.setEmail(params.getEmail());
        entity.setQq(params.getQq());entity.setDeptId(params.getDeptId());
        entity.setIsDeptManager(params.getIsDeptManager());
        entity.setIsManager(BaseConstant.YNEnum.NO.getCode());
        entity.addCreateParam(userInfo);
        entity.addEditParam(userInfo);
        userManageInfoService.save(entity);
        List<UserRoleInfo> userRoleList = new ArrayList<UserRoleInfo>();
        if(!CollectionUtils.isEmpty(params.getRoleCodes())){
            params.getRoleCodes().forEach(roleCode->{
                UserRoleInfo userRoleInfo = new UserRoleInfo();
                userRoleInfo.setUserId(entity.getId());
                userRoleInfo.setRoleCode(roleCode);
                userRoleInfo.addCreateParam(userInfo);
                userRoleInfo.addEditParam(userInfo);
                userRoleList.add(userRoleInfo);
            });
        }
        if(userRoleList.size()>0){
            userRoleInfoService.saveBatch(userRoleList);
        }
    }

    @Transactional
    @Override
    public void update(ManageUserUpdateInfoParam params, LoginSimpleUserInfo userInfo) {
        ValidatorUtils.validate(params);
        UserManageInfo userManageInfo= userManageInfoService.getById(params.getId());
        if(userManageInfo==null){
            throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
        }
        userManageInfo.setNickName(params.getNickName());
        userManageInfo.setRealName(params.getRealName());
        userManageInfo.setHeadImg(params.getHeadImg());
        userManageInfo.setMobile(params.getMobile());
        userManageInfo.setTel(params.getTel());
        userManageInfo.setEmail(params.getEmail());
        userManageInfo.setQq(params.getQq());
        userManageInfo.setDeptId(params.getDeptId());
        userManageInfo.setIsDeptManager(params.getIsDeptManager());
        userManageInfoService.updateById(userManageInfo);

        List<UserRoleInfo> userRoleList = new ArrayList<UserRoleInfo>();
        if(!CollectionUtils.isEmpty(params.getRoleCodes())){
            params.getRoleCodes().forEach(roleCode->{
                UserRoleInfo userRoleInfo = new UserRoleInfo();
                userRoleInfo.setUserId(userManageInfo.getId());
                userRoleInfo.setRoleCode(roleCode);
                userRoleInfo.addCreateParam(userInfo);
                userRoleInfo.addEditParam(userInfo);
                userRoleList.add(userRoleInfo);
            });
        }
        userRoleInfoService.remove(new QueryWrapper<UserRoleInfo>()
                .eq("user_id",userManageInfo.getId()));
        if(userRoleList.size()>0){
            userRoleInfoService.saveBatch(userRoleList);
        }
    }
}
